// Package service @author dingrui @since 2021/11/27
package service

import (
	"github.com/bannirui/mini-prize/dal/dao"
	"github.com/bannirui/mini-prize/dal/model"
)

var (
	G_resultService ResultService
)

type ResultService interface {
	GetAll(page, size int) []model.Result
	CountAll() int64
	GetNewPrize(size int, giftIds []int) []model.Result
	SearchByGift(giftId, page, size int) []model.Result
	SearchByUser(uid, page, size int) []model.Result
	CountByGift(giftId int) int64
	CountByUser(uid int) int64
	Get(id int) *model.Result
	Delete(id int) error
	Update(user *model.Result, columns []string) error
	Create(user *model.Result) error
}

type resultService struct {
	dao *dao.ResultDao
}

func init() {
	G_resultService = &resultService{
		dao: dao.G_resultDao,
	}
}

func (s *resultService) GetAll(page, size int) []model.Result {
	return s.dao.PageAll(page, size)
}

func (s *resultService) CountAll() int64 {
	return s.dao.CountAll()
}

func (s *resultService) GetNewPrize(size int, giftIds []int) []model.Result {
	return s.dao.ListByGiftIds(size, giftIds)
}

func (s *resultService) SearchByGift(giftId, page, size int) []model.Result {
	return s.dao.PageByGiftId(giftId, page, size)
}

func (s *resultService) SearchByUser(uid, page, size int) []model.Result {
	return s.dao.PageByUserId(uid, page, size)
}

func (s *resultService) CountByGift(giftId int) int64 {
	return s.dao.CountByGiftId(giftId)
}

func (s *resultService) CountByUser(uid int) int64 {
	return s.dao.CountByUserId(uid)
}

func (s *resultService) Get(id int) *model.Result {
	return s.dao.Get(id)
}

func (s *resultService) Delete(id int) error {
	return s.dao.Delete(id)
}

func (s *resultService) Update(data *model.Result, columns []string) error {
	return s.dao.Update(data, columns)
}

func (s *resultService) Create(data *model.Result) error {
	return s.dao.InsertOne(data)
}
